# vim: noexpandtab ft=python
import os, sys, re

# Accepted parameters (and defaults):
#
#   install_dir=/usr/local/sbin - directory where the install target copies the binaries
#   debug=1 - compilation style: 0 = optimize and strip, 1 = optimize with debug info, 2 = debug info, 3 = instrument functions

DESTDIR = ARGUMENTS.get('DESTDIR')

def arg_get(name, default):
	global DESTDIR
	arg = ARGUMENTS.get(name, default)
	if DESTDIR:
		if os.path.isabs(arg):
			arg = '.' + arg
		return os.path.join(DESTDIR, arg)
	else:
		return arg

install_dir = arg_get('install_dir', '/usr/local/sbin')
man_dir     = arg_get('man_dir',     '/usr/local/share/man/man8/')
cfg_dir     = arg_get('cfg_dir',     '/etc/zfs')

env = Environment()

f = os.popen('uname')
osname = f.readline().strip()
f.close()

env.CacheDir('/tmp/.zfs-fuse.scons')
env['CC'] = os.environ.setdefault("CC","gcc")
env['CCFLAGS'] = os.environ.setdefault("CFLAGS","")
env['LINKFLAGS'] = Split('-pipe -Wall')
env.Append(CCFLAGS = Split('-pipe -Wall -std=c99 -Wno-switch -Wno-unused -Wno-missing-braces -Wno-parentheses -Wno-uninitialized -Wcast-align -fno-strict-aliasing -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_REENTRANT -DTEXT_DOMAIN=\\"zfs-fuse\\" -O2 -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector -fstrict-volatile-bitfields --param=ssp-buffer-size=4 '))

if osname == "Linux":
  env.Append(CPPFLAGS = " -DLINUX_AIO")

debug = int(ARGUMENTS.get('debug', '0'))
optim = ARGUMENTS.get('optim', '-O2')

if not debug:
	env.Append(LINKFLAGS = ['-s'])
	env.Append(CCFLAGS = ['-s', optim, '-DNDEBUG'])
else:
	env.Append(LINKFLAGS = ['-ggdb'])
	env.Append(CCFLAGS = ['-ggdb'])
# Be careful here, one of DEBUG or NDEBUG must be defined, if you undef both, you'll get unexpected results
	if debug == 1:
		env.Append(CCFLAGS = [optim, '-DDEBUG'])
	elif debug == 2:
		env.Append(CCFLAGS = ['-DDEBUG'])
	elif debug == 3:
		env.Append(CCFLAGS = Split('-finstrument-functions -DDEBUG'))

if not (('-DDEBUG' in env['CCFLAGS']) or 
		('-DNDEBUG' in env['CCFLAGS'])):
	print
	print "Misconfigured debug level: Neither DEBUG or NDEBUG appears to have been defined: %s" % env['CCFLAGS']
	sys.exit(1)

env['CPPPATH'] = []

f = os.popen('uname -m')
arch = f.readline().strip()
f.close()

def getarch(arch):
	if arch == 'sparc64':
		return 'sparc64'

	if arch == 'x86_64':
		return 'amd64'

	if re.compile('i\d86').match(arch):
		return 'i386'

	return 'generic'

myarch = getarch(arch)

if not myarch:
	print
	print 'Sorry, only the x86, amd64 and sparc64 hardware architectures are supported'
	sys.exit(1)

if myarch == 'sparc64':
	env.Append(CCFLAGS = '-mcpu=ultrasparc')
	env.Append(ASFLAGS = '-mcpu=ultrasparc')

env['ARCH'] = ARGUMENTS.get('target', myarch)

if env['ARCH'] == 'i386' and myarch == 'amd64':
	env.Append(CCFLAGS = '-m32')
	env.Append(ASFLAGS = '-m32')
	env.Append(LINKFLAGS = '-m32')

Export('env')

SConscript('lib/libavl/SConscript')
SConscript('lib/libsolcompat/SConscript')
SConscript('lib/libnvpair/SConscript')
SConscript('lib/libumem/SConscript')
SConscript('lib/libuutil/SConscript')
SConscript('lib/libzfs/SConscript')
SConscript('lib/libzfscommon/SConscript')
SConscript('lib/libzpool/SConscript')
SConscript('lib/libsolkerncompat/SConscript')
SConscript('cmd/mount_zfs/SConscript')
SConscript('cmd/zdb/SConscript')
SConscript('cmd/ztest/SConscript')
SConscript('cmd/zpool/SConscript')
SConscript('cmd/zstreamdump/SConscript')
SConscript('cmd/zfs/SConscript')
SConscript('zfs-fuse/SConscript')

env.Install(install_dir, 'cmd/mount_zfs/mount.zfs')
env.Install(install_dir, 'cmd/zdb/zdb')
env.Install(install_dir, 'cmd/ztest/ztest')
env.Install(install_dir, 'cmd/zpool/zpool')
env.Install(install_dir, 'cmd/zfs/zfs')
env.Install(install_dir, 'zfs-fuse/zfs-fuse')
env.Install(install_dir, 'cmd/zstreamdump/zstreamdump')
env.Install(cfg_dir, '../contrib/zfs_pool_alert')

env.Install(man_dir, '../doc/zdb.8')
env.Install(man_dir, '../doc/zfs.8')
env.Install(man_dir, '../doc/zpool.8')
env.Install(man_dir, '../doc/zstreamdump.8')
env.Install(man_dir, '../doc/zfs-fuse.8')

if "tags" in sys.argv:
    print "updating tags..."
    os.system("ctags --extra=+f `find -name '*.c'` `find -name '*.h'`")

env.Alias('install', [install_dir, man_dir, cfg_dir])
